'''
Created on 2009-11-19

@author: Jialai_Zhu
'''
import os
import md5, hashlib
import zipfile 

class mufile(object):
    def __init__(self, sig_name, sig_content):
        self.md5u = sig_name
        self.md5f = sig_content
        
import configure            
def get_md5(data):
    m = md5.new()
    m.update(data)
    result = m.hexdigest()
    return result

class mufs(object):
    '''
    classdocs
    '''
    def __init__(self, dir=r'mufs'):
        '''
        Constructor
        '''
        self.dir = configure.dir + os.path.sep + dir;
        try: 
            os.mkdir(self.dir)
            configure.dir + os.path.sep + 'mufs';            
        except: pass
        
      
        
    def md5(self, str):
        return get_md5(str)
            
    def save(self, name, data, ext=None):
        filename = self.dir + os.sep + self.md5(name)
        if ext != None:
            filename = filename + '.' + ext
        f = file(filename, 'w')
        f.write(data)
        f.close()
        return mufile(self.md5(name), self.md5(data))
                    
    def name(self, filename):
        name = self.dir + os.sep + self.md5(filename)
        return name
    
    def rmdir(self, dir=None):
        if dir == None:
            dir = self.dir    
        self.dir_walk(dir, True, os.remove)    
                
    def read(self, url):
        return self.readmd5(self.md5(url))
    
    def dir_walk(self, dir, fullpath, handle):
        if dir == None:
            dir = self.dir
        files = os.listdir(dir) 
        for f in files:  
            try:
                if fullpath:
                    f = dir + os.sep + f
                handle(f)
            except:
                pass
                     
    def readmd5(self, md5):    
        data = ''
        filename = self.dir + os.sep + md5
        f = file(filename, 'r')
#        for i in f.readline():
#            data.join(i)
        data = f.read()
        f.close()
        return data 
    def store_hanlde(self, node):
        if node.code == '200':
            ret = self.save(node.url, node.page)
            node.md5u = ret.md5u
            node.md5f = ret.md5f
        else:
            node.md5f = ''
            node.md5u = self.md5(node.url)
                
    def zip(self, name):
        z = zipfile.ZipFile(name, 'w' , zipfile.ZIP_DEFLATED)
#        z.setpassword('virus')
        bak = os.getcwd()
        os.chdir(self.dir)
        self.dir_walk(self.dir, False, z.write)           
        os.chdir(bak)
        z.close()
    
    def extract(self, zfile, path=None):
        if path == None:
            path = configure.dir + os.path.sep + 'mufs' + os.sep;
        z = zipfile.ZipFile(zfile)
#        z.extractall(path)
        for zz in z.namelist():
            try:
                file(path + zz, 'wb').write(z.read(zz))
            except:
                pass    
#            print zz
        z.close()
       
        
